package com.ydc.ln.tutorial.j2se.ms;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.accessibility.internal.resources.accessibility;

/**
 * 循环、条件、概率测试
 * 
 * @author ydc
 * @date 上午11:24:39 2013-7-11
 */
public class Ch07Test {

    public static int count = 0;

    public FileOutputStream fos = null;

    public static void main(String[] args) {
        new Ch07Test().test003();
        // System.out.println("abc".substring(0,3));
    }

    /**
     * 1,2,2,3,4打印这5个数的所有不同排列 void
     */
    public void test003() {
        String s = "abc";

        // int[] index = new int[s.length()];
        // for (int i=0; i<s.length(); i++) {
        // index[i] = s.indexOf(s.charAt(i));
        // }
        pailie(s, "");
    }

    public void pailie(String s, String p) {
        if (s.length() == 0) {
            count++;
            System.out.println(count + "\t" + p);
        } else {
            int[] index = new int[s.length()];
            for (int i = 0; i < s.length(); i++) {
                index[i] = s.indexOf(s.charAt(i));
                // System.out.println("i=" + i + "\ts.charAt(i)=" + s.charAt(i)
                   //      + "\ts.indexOf(s.charAt(i))=" + s.indexOf(s.charAt(i)));
            }
            // System.out.println("------------------");
            for (int i = 0; i < s.length(); i++) {
                if (i == index[i]) {
                    pailie(s.substring(1), p + s.substring(0, 1));
                }
                s = s.substring(1) + s.substring(0, 1);
            }
        }
    }


    /**
     * 字符串(由26个小写英文字母组成)中，每个字母出现的次数，并排序 void
     */
    public void test002() {
        test002_1("yangdachao");
    }

    public void test002_1(String str) {
        Integer[] tongji = new Integer[26];

        char[] chars = str.toCharArray();
        int len = chars.length;
        for (int i = 0; i < len; i++) {
            char c = chars[i];
            int index = c - 97;
            if (tongji[index] == null) {
                tongji[index] = 1;
            } else {
                tongji[index]++;
            }
        }

        for (int i = 0; i < 26; i++) {
            if (tongji[i] != null)
                System.out.println((char) (i + 97) + "\t" + tongji[i]);
        }
    }



    /**
     * 把一个数组里的数全部列出来 void
     */
    public void test001() {

        try {
            fos = new FileOutputStream(new File("out001.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        }

        showAll("12234", "");

        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {

        }
    }

    public void showAll(String str1, String str2) {

        int len = str1.length();
        if (len == 0) {
            System.out.println(++count + "\t" + str2);
            write2File(count + "\t" + str2);
        } else {
            for (int i = 0; i < len; i++) {
                String tmp1 = str1.substring(0, i) + str1.substring(i + 1, len);
                String tmp2 = str2 + str1.charAt(i);
                // System.out.println(tmp2);
                // write2File(tmp2);
                showAll(tmp1, tmp2);
            }
        }
    }


    public void write2File(String str) {
        try {
            str += "\n";
            fos.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
